package com.kk.test;

import com.kk.HelloApp.Hello;
import com.kk.HelloApp.HelloHelper;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

/**
 * 此类为服务端的入口类，把服务注册到ORB
 */
public class HelloServer {
    public static void main(String args[]) {
        try {
            //获取ORB实例
            ORB orb = ORB.init(args, null);

            //固定写法
            POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
            //激活
            rootpoa.the_POAManager().activate();

            //创建一个HelloImpl服务（servant），并注册到ORB上
            HelloImpl helloImpl = new HelloImpl();
            helloImpl.setORB(orb);

            //从服务中得到对象的引用
            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
            Hello href = HelloHelper.narrow(ref);

            //得到一个根名称的上下文
            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            //在命名上下文中绑定对象
            String name = "Hello";
            NameComponent path[] = ncRef.to_name(name);
            ncRef.rebind(path, href);

            System.out.println("HelloServer ready and waiting ...");

            //启动线程服务，等待客户端的调用
            orb.run();
        } catch (Exception e) {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
        }
        System.out.println("HelloServer Exiting ...");
    }
}